
<template>
  <!-- 商品评价 -->
  <div class="pos-r full-height">
    <van-nav-bar
      fixed
      title="评价"
      left-arrow
      class="bg-primary color-white"
      @click-left="closeEvaluate"
    />
    <div id="evaluateScroll" ref="evaluateScroll" class="has-top plr-30 pb-30 ofs pos-r full-height">
      <p class="fw-b fs-28 pt-30">全部评价({{ recordAmout }})</p>
      <evaluateList :evaluate-list="evaluateList" />
    </div>
  </div>

</template>

<script>
import evaluateList from 'components/productDetail/evaluateList'
import { GetCommentListByProduct } from 'api/product'
export default {
  name: 'ProductEvaluatePopup',
  components: {
    evaluateList
  },
  props: {
    productId: { // 商品ID
      type: Number,
      required: true,
      default: 0
    }
  },
  data() {
    return {
      evaluateList: [],
      showAllTab: false,
      commentRequest: {
        page: 1,
        size: 4
      },
      recordAmout: 0,
      totalAmout: 0,
      scollRequest: true,
      end: false
    }
  },
  computed: {

  },
  watch: {
    productId(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.init()
      }
    }
  },
  mounted() {
    this.init()
  },

  methods: {
    init() {
      this.evaluateList = []
      this.totalAmout = 0
      this.scollRequest = true
      this.end = false
      this.commentRequest.page = 1
      this.commentRequest.size = 4
      this.commentRequest.product_id = this.productId
      this.GetCommentListByProduct()
    },
    closeEvaluate() {
      this.$emit('closeEvaluate')
    },
    // 滚动事件触发下拉加载
    eScroll() {
      const _this = this
      if (_this.scollRequest === true) {
        if (this.$utils.getScrollHeight('evaluateScroll') - this.$utils.getClientHeight('evaluateScroll') - this.$utils.getScrollTop('evaluateScroll') <= 5) {
          _this.scollRequest = false
          // alert('触发！！！')
          console.log('加载')
          _this.scollTop = this.$utils.getScrollTop('evaluateScroll')
          if (_this.commentRequest.page < _this.totalAmout) {
            _this.commentRequest.page++
            _this.GetCommentListByProduct()
          } else {
            console.log('end')
            _this.end = true
          }
          // }
        }
      }
    },
    // 获取评价
    GetCommentListByProduct() {
      GetCommentListByProduct(this.commentRequest).then(res => {
        if (res && res.Data) {
          console.log(res)
          // this.evaluateData = res.Data
          this.totalAmout = res.Data.TotalAmout
          this.recordAmout = res.Data.RecordAmout
          const _list = res.Data.Data
          if (_list && _list.length > 0) {
            _list.forEach(item => {
              if (item.Images) {
                item.ImagesList = item.Images.split('|')
              }
              this.evaluateList.push(item)
            })
          }
        }
        this.$nextTick(function() {
          console.log(this.$utils.getScrollHeight('evaluateScroll'))
          this.$refs.evaluateScroll.addEventListener('scroll', this.eScroll)
        })
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.color-white {
  /deep/.van-icon {
    color: #fff;
  }
  /deep/.van-nav-bar__title {
    color: #fff;
  }
}
// 评价
.product-evaluate{
  background: $colorF;
  padding: 30px 32px;
  .arrow{
    position: relative;
    padding-right: 20px;
    &:after{
      content:'';
      position: absolute;
      top: 10px;
      right: 0;
      width: 12px;
      height: 12px;
      border: 2px solid;
      border-color:$color9 $color9 transparent transparent;
      transform: rotate(45deg);
    }
  }
  .evaluate-item{
    padding: 30px 0;
     &:not(:first-child){
      border-top: 1px solid $colorBorder;
     }
     &:last-child{
       padding-bottom: 0;
     }
    .item-head{
      @include flex-content(flex-start, center);
    }
    .shop-img{
      .img{
        display: inline-block;
        width:152px;
        height: 152px;
        object-fit: cover;
        margin-top: 30px;
        margin-right: 26px;
        &:nth-child(4n){
          margin-right: 0;
        }
      }
    }
    .evaluate-item-replay{
      padding: 24px;
      background: $colorF6;
      margin-top: 20px;
      border-radius: 8px;
      position: relative;
      &:after{
        content: "";
        position: absolute;
        width: 0;
        height: 0;
        border: 10px solid;
        border-color: transparent transparent $colorF6;
        top: -20px;
        left: 40px;
      }
    }
  }
}
</style>
